using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Rss;

public partial class admin_webparts_Plug_ins_RssNews_RssMenu : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string g = Request.QueryString["g"];
            string action = Request.QueryString["action"];
            string controlName = string.Empty;

            if (g != null)
            {
                if ("addmenu".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    controlName = "RssMenuInput.ascx";
                }
                else if ("editmenu".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    controlName = "RssMenuInput.ascx";
                }
                else if ("removemenu".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    if (Request.QueryString["m"] != null)
                    {
                        this.RemoveMenu(Request.QueryString["m"].Trim());
                    }

                    string bUrl = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&item=" + Request.QueryString["item"] + (Request.QueryString["g"] == null ? "" : "&g=" + Request.QueryString["g"]);
                    Response.Redirect(bUrl, false);
                    return;
                }
                else if ("detail".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    controlName = "RssMenuDetail.ascx";
                }
                else if ("addlink".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    controlName = "RssMenuAddLink.ascx";
                }
                else if ("removelink".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    int menuID = Convert.ToInt32(Request.QueryString["m"].Trim());
                    int linkID = Convert.ToInt32(Request.QueryString["link"].Trim());
                    this.RemoveLinkFromMenu(menuID, linkID);

                    string bUrl = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&item=" + Request.QueryString["item"] + "&g=" + Request.QueryString["g"] + "&m=" + Request.QueryString["m"] + "&action=detail";
                    Response.Redirect(bUrl, false);
                    return;
                }
                

                if (controlName == string.Empty)
                {
                    controlName = "RssMenuList.ascx";
                }
            }

            if (controlName == string.Empty)
            {
                controlName = "RssGroupList.ascx";
            }

            Control ctrl = this.LoadControl(controlName);
            this.Controls.Add(ctrl);
        }
        catch (Exception ex)
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("div");
            ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            this.Controls.Add(ctrl);
        }
    }

    public void RemoveMenu(string idStr)
    {
        try
        {
            int id = Convert.ToInt32(idStr);
            RssMenu menu = new RssMenu(id);
            if (menu.ID > 0)
            {
                menu.Delete();
            }
        }
        catch { }
    }

    public void RemoveLinkFromMenu(int menuID, int linkID)
    {
        try
        {
            RssMenuLink menuLink = new RssMenuLink(menuID, linkID);
            if (menuLink.ID > 0)
            {
                menuLink.Delete();
            }
        }
        catch { }
    }
}
